package com.simon.handler;

import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;

import com.simon.exception.CheckException;
import com.simon.exception.OperationFailedException;
import com.simon.exception.PermissonException;
import com.simon.exception.TokenException;
import com.simon.http.model.ExResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.http.converter.HttpMessageNotReadableException;

import com.simon.utils.SystemCode;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class GlobalExceptionHandler {

	private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);


	/**
	 *处理参数错误异常
	 * @param exception
	 * @return
	 */
	@ExceptionHandler(MethodArgumentNotValidException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public ExResponse handle(Exception exception) {
		if(exception instanceof MethodArgumentNotValidException){
			MethodArgumentNotValidException exs = (MethodArgumentNotValidException) exception;
			String error = exs.getBindingResult().getFieldError().getDefaultMessage();
			LOGGER.info("参数绑定错误信息:{}",error);
			ExResponse response = new ExResponse();
			response.setCode(SystemCode.SYSTEM_EXCEPTION_CODE);
			response.setMsg(error);
			return response;
		}
		return null;
	}

	@ExceptionHandler(HttpMessageNotReadableException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public ExResponse handlerHttpMessageNotReadableException(Exception exception) {
		if(exception instanceof HttpMessageNotReadableException){
			HttpMessageNotReadableException exs = (HttpMessageNotReadableException) exception;
			String error = exs.getLocalizedMessage();
			LOGGER.error("错误信息:{}",error);
			ExResponse response = new ExResponse();
			response.setCode(SystemCode.SYSTEM_EXCEPTION_CODE);
			response.setMsg("无可读取的参数体");
			return response;
		}
		return null;
	}

	@ExceptionHandler(MissingServletRequestParameterException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public ExResponse handlerMissingServletRequestParameterException(Exception exception) {
		if(exception instanceof MissingServletRequestParameterException){
			MissingServletRequestParameterException exs = (MissingServletRequestParameterException) exception;
			String error = exs.getLocalizedMessage();
			LOGGER.error("错误信息:{}",error);
			ExResponse response = new ExResponse();
			response.setCode(SystemCode.SYSTEM_EXCEPTION_CODE);
			response.setMsg("参数不能为空");
			return response;
		}
		return null;
	}


	@ExceptionHandler(BindException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public ExResponse handlerBindException(Exception exception) {
		if(exception instanceof MissingServletRequestParameterException){
			BindException exs = (BindException) exception;
			String error = exs.getLocalizedMessage();
			LOGGER.error("错误信息:{}",error);
			ExResponse response = new ExResponse();
			response.setCode(SystemCode.SYSTEM_EXCEPTION_CODE);
			response.setMsg("参数绑定失败");
			return response;
		}
		return null;
	}

	@ExceptionHandler(ConstraintViolationException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public ExResponse handlerConstraintViolationException(Exception exception) {
		if(exception instanceof ConstraintViolationException){
			ConstraintViolationException exs = (ConstraintViolationException) exception;
			String error = exs.getLocalizedMessage();
			LOGGER.error("错误信息:{}",error);
			ExResponse response = new ExResponse();
			response.setCode(SystemCode.SYSTEM_EXCEPTION_CODE);
			response.setMsg("参数验证失败");
			return response;
		}
		return null;
	}

	@ExceptionHandler(ValidationException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public ExResponse handlerValidationException(Exception exception) {
		if(exception instanceof ValidationException){
			ValidationException exs = (ValidationException) exception;
			String error = exs.getLocalizedMessage();
			LOGGER.error("错误信息:{}",error);
			ExResponse response = new ExResponse();
			response.setCode(SystemCode.SYSTEM_EXCEPTION_CODE);
			response.setMsg("参数验证失败");
			return response;
		}
		return null;
	}

	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public ExResponse handlerHttpRequestMethodNotSupportedException(Exception exception) {
		if(exception instanceof HttpRequestMethodNotSupportedException){
			HttpRequestMethodNotSupportedException exs = (HttpRequestMethodNotSupportedException) exception;
			String error = exs.getLocalizedMessage();
			LOGGER.error("错误信息:{}",error);
			ExResponse response = new ExResponse();
			response.setCode(SystemCode.SYSTEM_EXCEPTION_CODE);
			response.setMsg("不支持当前请求方法");
			return response;
		}
		return null;
	}

	@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public ExResponse handlerHttpMediaTypeNotSupportedException(Exception exception) {
		if(exception instanceof HttpMediaTypeNotSupportedException){
			HttpMediaTypeNotSupportedException exs = (HttpMediaTypeNotSupportedException) exception;
			String error = exs.getLocalizedMessage();
			LOGGER.error("错误信息:{}",error);
			ExResponse response = new ExResponse();
			response.setCode(SystemCode.SYSTEM_EXCEPTION_CODE);
			response.setMsg("不支持当前媒体类型");
			return response;
		}
		return null;
	}

	@ExceptionHandler(InvalidDataAccessApiUsageException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public ExResponse handlerInvalidDataAccessApiUsageException(Exception exception) {
		if(exception instanceof InvalidDataAccessApiUsageException){
			InvalidDataAccessApiUsageException exs = (InvalidDataAccessApiUsageException) exception;
			String error = exs.getLocalizedMessage();
			LOGGER.error("错误信息:{}",error);
			ExResponse response = new ExResponse();
			response.setCode(SystemCode.SYSTEM_EXCEPTION_CODE);
			response.setMsg("操作失败");
			return response;
		}
		return null;
	}


	/**
	 * 处理业务层的异常---自定义
	 * @param exception
	 * @return
	 */
	@ExceptionHandler(OperationFailedException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public ExResponse handler(Exception exception) {
		if(exception instanceof OperationFailedException){
			OperationFailedException exs = (OperationFailedException) exception;
			String error = exs.getLocalizedMessage();
			LOGGER.info("自定义错误信息:{}",error);
			ExResponse response = new ExResponse();
			response.setCode(SystemCode.SYSTEM_EXCEPTION_CODE);
			response.setMsg(error);
			return response;
		}
		return null;
	}

	/**
	 * 身份令牌验证异常处理--自定义
	 * @param exception
	 * @return
	 */
	@ExceptionHandler(TokenException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public ExResponse handler(TokenException exception) {
		String error = exception.getLocalizedMessage();
		LOGGER.info("身份令牌错误信息:{}", error);
		ExResponse response = new ExResponse();
		response.setCode(SystemCode.SYSTEM_EXCEPTION_CODE);
		response.setMsg(error);
		return response;
	}

	/**
	 * 权限验证异常处理--自定义
	 * @param exception
	 * @return
	 */
	@ExceptionHandler(PermissonException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public ExResponse handler(PermissonException exception) {
		String error = exception.getLocalizedMessage();
		LOGGER.info("权限验证错误信息:{}", error);
		ExResponse response = new ExResponse();
		response.setCode(SystemCode.SYSTEM_EXCEPTION_CODE);
		response.setMsg(error);
		return response;
	}

	/**
	 * 参数验证异常处理--自定义
	 * @param exception
	 * @return
	 */
	@ExceptionHandler(CheckException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public ExResponse handler(CheckException exception) {
		String error = exception.getLocalizedMessage();
		LOGGER.info("参数验证错误信息:{}", error);
		ExResponse response = new ExResponse();
		response.setCode(SystemCode.SYSTEM_EXCEPTION_CODE);
		response.setMsg(error);
		return response;
	}


//	@ExceptionHandler(value=Exception.class)
//	@ResponseBody
//	public ResponseEntity<?> errorHandler(HttpServletRequest req,Exception e) throws Exception{
//		ExResponse response = new ExResponse();
//		//异常码
//		response.setCode(SystemCode.SYSTEM_EXCEPTION_CODE);
//		response.setMsg(e.getMessage());
//		return ResponseEntity.ok(response);
//	}
}
